Spring cloud Hystrix 熔断实现


通常回退方法的实现有两种,一种是通过 feign 来进行实现,另一种解决方案则是根据 @HystrixCommand 注解进行实现,在此之前我们需要添加依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

之后在启动类中添加注解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
* 启动类,分别开启 consul、feign 以及服务熔断
*
* @author kunlun
* @date 2021/7/5
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

尽管 @EnableCircuitBreaker3.0.1 开始 Hystrix 就不在 Spring cloud Netfilx 中了,但是他是唯一一个实现断路器的注解

https://martinfowler.com/bliki/CircuitBreaker.html

application.properties

1
2
3
4
5
6
7
spring.application.name=Hystrix
server.port=9900
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=service-provider
spring.cloud.consul.discovery.register=false
feign.hystrix.enabled=true # 开启 Hystrix

service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.demo.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
* 使 Feign 支持 Hystrix,即添加 fallback 处理类,在服务熔断时返回 fallback 类中内容
*
* @author kunlun
* @date 2021/7/08
*/
@FeignClient("service-provider")
public interface MyFeignFallbackClient {

/**
* 添加指定的 fallback 类,在服务熔断时返回 fallback 类内容
* @return
*/
@GetMapping("/hey")
String hey();
}

controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.demo.controller;

import com.example.demo.service.MyFeignFallbackClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 调用 FeiginFallback 客户端
*
* @author kunlun
* @date 2021/7/5
*/
@RestController
public class HeyController {

@Autowired
private MyFeignFallbackClient myFeignFallbackClient;

@RequestMapping("/hey")
@HystrixCommand(fallbackMethod = "defaultCities")
public String index() {
String fallback = myFeignFallbackClient.hey();
return fallback;
}

public String defaultCities() {
return "No server!!!";
}

}

本文使用《江雪分析公开知识存储库知识共享许可证》进行发布